<template>
  <el-dialog v-model="dialogVisible" title="新增负责人" width="600">
    <el-form
      ref="formRef"
      :model="form"
      label-width="auto"
      label-position="right"
      class="demo-form-inline"
      :rules="formRules"
      status-icon
    >
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="物品名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入物品名称"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数量" prop="number">
            <el-input
              v-model="form.number"
              placeholder="请输入数量"
              type="number"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单件体积" prop="volume">
            <el-input
              v-model="form.volume"
              placeholder="请输入单件体积"
              type="number"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属类别" prop="category_id">
            <el-select v-model="form.categoryId" placeholder="请选择类别">
              <el-option
                v-for="item in categoryOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="入库仓库" prop="stash_id">
            <el-select v-model="form.stashId" placeholder="请选择入库仓库">
              <el-option
                v-for="item in stashOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              placeholder="请输入备注"
              type="textarea"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item>
            <el-button type="primary" @click="submit">确定</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { ref, nextTick, onMounted } from "vue";
import { formRules } from "./rules.js";
import { addEnter, setEnter } from "@/api/enterRecord.ts";
import { getCategory } from "@/api/category.ts";
import { getStash } from "@/api/stash.ts";

const dialogVisible = ref(false);

const form = ref({
  name: "",
  number: null,
  volume: null,
  categoryId: null,
  stashId: null,
  remark: null
});
const isEdit = ref();
const formRef = ref();
const categoryOptions = ref();
const stashOptions = ref();

onMounted(() => {
  getCategoryList();
  getStashList();
});

const getCategoryList = async () => {
  const res = await getCategory({
    page: 1,
    pageSize: 100
  });
  categoryOptions.value = res.items;
};
const getStashList = async () => {
  const res = await getStash({
    page: 1,
    pageSize: 100
  });
  stashOptions.value = res.items;
};

const submit = async () => {
  if (!formRef.value) return;
  await formRef.value.validate(async (valid, fields) => {
    if (valid) {
      if (isEdit.value) {
        await setEnter({
          ...form.value
        });
      } else {
        await addEnter({
          ...form.value
        });
      }
      closeDialog();
    } else {
      console.log("error submit!", fields);
    }
  });
};

// 打开对话框
const openDialog = async (row = undefined) => {
  dialogVisible.value = true;
  await nextTick();
  formRef.value.resetFields();
  isEdit.value = row ? true : false;
  if (row) {
    console.log("row", row);
    form.value = { ...row };
  }
};

const emits = defineEmits(["closeDialogEvent"]);
const closeDialog = () => {
  dialogVisible.value = false;
  emits("closeDialogEvent");
};

defineExpose({
  openDialog
});
</script>

<style lang="less" scoped></style>
